package com.kdgc.power.fw.annotation;

import org.beetl.sql.annotation.builder.AttributeConvert;
import org.beetl.sql.clazz.SQLType;
import org.beetl.sql.clazz.kit.BeanKit;
import org.beetl.sql.clazz.kit.BeetlSQLException;
import org.beetl.sql.core.ExecuteContext;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.UUID;


@SuppressWarnings("AliMissingOverrideAnnotation")
public class UUIDCodeConvert implements AttributeConvert {
    @Override
    public Object toDb(ExecuteContext ctx, Class cls, String name, Object dbValue) {
        SQLType sqlType = ctx.sqlSource.getSqlType();
        //判断是否insert语句
        if (SQLType.INSERT == sqlType) {
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            BeanKit.setBeanProperty(dbValue, uuid, name);
            return uuid;
        } else {
            return BeanKit.getBeanProperty(dbValue, name);
        }
    }

    @Override
    public Object toAttr(ExecuteContext ctx, Class cls, String name, ResultSet rs, int index) throws SQLException {
        return rs.getString(index);
    }


}
